//
//  RoomNameViewController.m
//  CMVRFForIOS
//
//  Created by zhangnorman on 15/4/14.
//  Copyright (c) 2015年 hvacapp. All rights reserved.
//

#import "RoomNameViewController.h"
#import "EditorTableViewCell.h"
#import <TPKeyboardAvoidingTableView.h>
#import "RoomTypeGroupDataResp.h"
#import "UITapView.h"

@interface RoomNameViewController ()

@property (nonatomic,strong) TPKeyboardAvoidingTableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,strong) NSIndexPath *selectIndexPath;
@property (nonatomic,strong) RoomData *selectedRoom;

@end

@implementation RoomNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initData];
    [self setupViews];
    [self loadData];
}

#pragma mark - 数据处理
/**
 *  初始化数据
 */
- (void)initData {
    self.dataSource = [NSMutableArray new];
    self.selectedRoom = nil;
}

- (void)loadData {
    [HttpClient getWithUrl:HTTP_GET_PROJECT_ROOMS_ALL_TYPE paramDic:[NSMutableDictionary new] resultClass:[RoomTypeGroupDataResp class] success:^(RoomTypeGroupDataResp *obj) {
        if (obj.result != nil && obj.result.count > 0) {
            [self.dataSource addObjectsFromArray:obj.result];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        CMLog(@"获取房间类型失败：%@", error);
    }];
}


#pragma mark - 初始化视图
- (void)setupViews{
    self.title = @"选择房间";
    
    //列表
    self.tableView = [[TPKeyboardAvoidingTableView alloc] initWithFrame:[UIView frameWithOutNav] style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}


#pragma mark - 事件响应
//- (void)onSave {
//    if (self.delegate != nil) {
//        [self.delegate finishedSelected:@""];
//    }
//    [self.navigationController popViewControllerAnimated:YES];
//}


#pragma mark - tableView显示设置数据
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//    if (self.dataSource.count > 0) {
//        return 2;
//    }
    return 1;//增加一个自定义编辑房间名的section
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count + 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == self.dataSource.count) {
        //当前是要编辑的cell
        EditorTableViewCell *cell = [[EditorTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"editCell"];
        [cell setTitleStr:@"自定义" andColor:nil andHintStr:@"请输入房间名" andInputViewIsHidden:NO andInfoStr:nil andUnit:nil];
        cell.TextFiledEditingEndBlock = ^(NSString *value){
            RoomData *room = [RoomData new];
            room.room_name = [NSString stringWithString:value];
            room.room_type = [NSString stringWithString:value];
//            room.guidId = EMPTY_DEVICE_ID;
            self.selectedRoom = room;
            if (self.userSelectedBlock != nil) {
                self.userSelectedBlock(self.selectedRoom);
            }
            [self.navigationController popViewControllerAnimated:YES];
        };
        return cell;
    }
    else {
        static NSString *identifier = @"RoomNameCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
            //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        cell.textLabel.textColor = CM_TABLE_TITLE_COLOR;
        cell.textLabel.font = [UIFont systemFontOfSize:kListTitleFontSize];
        RoomTypeGroupDataResp *data = [self.dataSource objectAtIndex:indexPath.row];
        cell.textLabel.text = data.Name;
        return cell;
    }
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == self.dataSource.count) {
        //当前是要编辑的cell
        if (self.selectedRoom == nil) {
            [Tools showMessageCenter:@"请输入房间名称"];
            return;
        }
    }
    else {
        RoomTypeGroupDataResp *data = [self.dataSource objectAtIndex:indexPath.row];
        RoomData *room = [RoomData new];
        room.room_name = [NSString stringWithString:data.Name];
        room.room_type = [NSString stringWithString:data.Name];
        self.selectedRoom = room;
    }
    
    if (self.userSelectedBlock != nil) {
        self.userSelectedBlock(self.selectedRoom);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"RoomNameCell";
    if (indexPath.section == 1 || self.dataSource.count == 0) {
        //当前是要编辑的cell
        EditorTableViewCell *cell = [[EditorTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"editCell"];
        return [cell getHeight];
        
    }
    else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
            //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        return cell.height;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.000001;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
